package com.mac.sskj.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import java.util.HashMap;
import com.baomidou.mybatisplus.plugins.Page;
import com.mac.sskj.service.TestService;
import com.mac.sskj.entity.Test;


/**
 * @Created：2020-06-28
 * @Author zhangshoucang
 * @Version:1.0
 * @Description:Test
 * @Email:303417292@qq.com
*/

@Controller
@RequestMapping("/test")
public class TestController
{

    @Autowired 
	TestService testService;

	@RequestMapping(value="/listPage")
	public Object listPage(HttpServletRequest request ,HttpServletResponse response)
	{
		return "/sskj/test/list";
	}

	/**
	 * @param pageNum  第几页
	 * @param orderField 排序字段
	 * @param ascStr  顺序还是倒序
	 * @return page 插件mysqlplus 封装的分页对象
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public Object list(HttpServletRequest request ,HttpServletResponse response,
						Integer pageNum, Integer pageSize, String orderField, String ascStr, Integer id)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		pageNum = pageNum + 1; //前端从0开始
		Page page = new Page(pageNum, pageSize);
		page.setOrderByField(orderField);
		if("desc".equals(ascStr))
		{
			page.setAsc(false);
		}
		page.setRecords(testService.findList(page, params));

		return page;
	}

	@ResponseBody
	@RequestMapping(value="/listAll")
	public Object list(HttpServletRequest request ,HttpServletResponse response)
	{

		return testService.selectList(null);
	}

	@RequestMapping(value="/addPage")
	public Object addPage(HttpServletRequest request ,HttpServletResponse response)
	{
		return "/sskj/test/add";
	}

	@ResponseBody
	@RequestMapping(value="/add")
	public Object add(HttpServletRequest request ,HttpServletResponse response, Test test)
	{
		test.setId(new Integer(0));
		testService.insert(test);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resCode", "1");
		map.put("resMsg", "新增成功");
		return map;
	}

	@RequestMapping(value="/editPage")
	public Object editPage(HttpServletRequest request ,HttpServletResponse response, Integer id)
	{
		Test test = testService.selectById(id);
		request.setAttribute("test", test);
		return "/sskj/test/edit";
	}

	@ResponseBody
	@RequestMapping(value="/edit")
	public Object edit(HttpServletRequest request ,HttpServletResponse response, Test test)
	{
		testService.updateById(test);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resCode", "1");
		map.put("resMsg", "修改成功");
		return map;
	}

	@ResponseBody
	@RequestMapping(value="/delete")
	public Object delete(HttpServletRequest request ,HttpServletResponse response, Integer id)
	{
		testService.deleteById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resCode", "1");
		map.put("resMsg", "删除成功");
		return map;
	}
}
